<script setup lang="ts">
import useToast from '@/hooks/useToast';

const { showToast } = useToast();
const props = defineProps<{ data?: IMoneyManagementList | IOrderDetailInfo }>();
const collapseNames = ref<number[]>([1]);

const copyContent = () => {
  uni.setClipboardData({
    data: props.data!.orderNo,
    success: (res) => {
      showToast('复制成功', 'success');
    },
  });
};

const countTotalPrice = () => {
  if (isOrderDetail(props.data!) && props.data!.promoteGain) {
    return parseFloat((props.data.realAmount + props.data.promoteGain).toFixed(2));
  } else {
    return parseFloat(props.data!.realAmount.toFixed(2));
  }
};

const isOrderDetail = (data: IMoneyManagementList | IOrderDetailInfo | undefined): data is IOrderDetailInfo => {
  return (props.data as IOrderDetailInfo).orderState !== undefined;
};
</script>
<template>
  <view v-if="props.data">
    <van-cell-group inset>
      <view class="c-color-black fw-bold bg-white c-pd pb-0">
        {{ props.data!.businessName }}
      </view>
      <view v-for="(item, index) in props.data!.goodsList" :key="index">
        <van-card thumb-mode="aspectFill" :thumb="item.images" :price="Number(item.activeDiscount ? item.activePrice : item.salePrice * item.nums).toFixed(2)" :num="item.nums" :tag="item.activeDiscount ? '折' : ''">
          <view slot="title" class="c-text-12 mb-3 fw-bold c-color-black">{{ item.name }}</view>
          <view class="c-text-color-grey" slot="desc" v-if="item.strSpecVal && item.strSpecVal != '无'">
            {{ item.strSpecVal }}
          </view>
        </van-card>
        <van-card thumb-mode="aspectFill" v-for="(cItem, cIndex) in item.oGRelations" :key="cIndex" :thumb="cItem.images">
          <view slot="title" class="c-text-12 mb-3">{{ cItem.name }}</view>
          <view class="c-text-color-grey" slot="desc">{{ cItem.sku }} </view>
        </van-card>
        <van-divider v-if="index + 1 < props.data!.goodsList.length"></van-divider>
      </view>
      <van-cell title="订单编号" use-label-slot>
        <view slot="right-icon">
          <text class="c-text-color-grey font-size-3 mr-1">{{ props.data!.orderNo }}</text>
          <van-button type="primary" size="mini" @click="copyContent">复制</van-button>
        </view>
      </van-cell>
      <van-cell title="预计送达时间" v-if="isOrderDetail(props.data) && props.data.orderState <= 11 && props.data.expectedTime && props.data.serviceMode == 1" use-label-slot> </van-cell>
      <van-cell title="包装费" :value="'￥' + props.data!.packageAmount" use-label-slot> </van-cell>
      <van-cell title="配送费" :value="'￥' + props.data!.deliveryAmount" use-label-slot> </van-cell>
      <van-cell title="养老金抵扣额" v-if="isOrderDetail(props.data) && props.data.discountOld" :value="'￥' + props.data!.discountOld" use-label-slot> </van-cell>
      <van-cell title="养老金返利额" v-if="isOrderDetail(props.data) && props.data.rebateOld" :value="'￥' + props.data!.rebateOld" use-label-slot> </van-cell>
      <van-cell title="物业金抵扣额" v-if="isOrderDetail(props.data) && props.data.discountPmc" :value="'￥' + props.data!.discountPmc" use-label-slot> </van-cell>
      <van-cell title="物业金返利额" v-if="isOrderDetail(props.data) && props.data.rebatePmc" :value="'￥' + props.data!.rebatePmc" use-label-slot> </van-cell>
      <van-cell title="押金" v-if="isOrderDetail(props.data) && props.data.depositAmount" :value="'￥' + props.data!.depositAmount" use-label-slot> </van-cell>
      <van-cell title="差价" v-if="props.data.lastAmount" :value="'￥' + props.data!.lastAmount" use-label-slot> </van-cell>
      <van-collapse :border="false" :value="collapseNames" v-if="isOrderDetail(props.data) && props.data.promoteGain" @change="collapseNames = $event.detail">
        <van-collapse-item clickable title=" " :is-link="false">
          <van-cell-group :border="false">
            <van-cell title="推广优惠金额" :value="'-￥' + props.data.promoteGain" />
            <van-cell title="实际支付" :value="'￥' + props.data.realAmount" />
          </van-cell-group>
          <view slot="right-icon" class="flex items-center">
            <text class="font-size-3">合计 </text>
            <text class="font-bold font-size-4">￥{{ countTotalPrice() }}</text>
            <i v-if="isOrderDetail(props.data) && props.data.promoteGain" class="i-carbon:chevron-down font-size-5 ml-1"></i>
          </view>
        </van-collapse-item>
      </van-collapse>
    </van-cell-group>
  </view>
</template>
